home *** CD-ROM | disk | FTP | other *** search
- unit Textio;
- { PC Plus sample program to illustrate text file IO. }
- { It opens one file for reading, another for writing and copies the }
- { contents, one char at a time, between the files. }
- { Just to add to the fun, it also 'encrypts' the text en route. }
- { The encryption is *very* simple. So don't bother trying to sell }
- { this program to the CIA! }
-
- interface
-
- uses
- SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
- Forms, Dialogs, StdCtrls;
-
- type
- TForm1 = class(TForm)
- EncryptBtn: TButton;
- DecryptBtn: TButton;
- procedure EncryptBtnClick(Sender: TObject);
- procedure DecryptBtnClick(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
-
- const
- MAGICNUM = 17; { this is an arbitrary 'encryption factor' }
-
- var
- Form1: TForm1;
-
- implementation
-
- {$R *.DFM}
-
- procedure TForm1.EncryptBtnClick(Sender: TObject);
- var
- infile,
- outfile : TextFile;
- INfilename,
- OUTfilename : string;
- ch : char;
- begin
- INfilename := 'TextIO.pas';
- OUTfilename := 'Encrypt.txt';
- if not FileExists( INfilename ) then { Check that input file exists }
- ShowMessage('File: ' + INfilename + ' not found!')
- else
- begin
- AssignFile(infile, INfilename);
- Reset(infile);
- AssignFile(outfile, OUTfilename);
- Rewrite(outfile);
- while not Eof(infile) do
- begin
- Read(infile, ch);
- { Write(outfile, ch); }
- Write(outfile, Chr(Ord(ch)+MAGICNUM));
- end;
- CloseFile(outfile);
- CloseFile(infile);
- end;
- end;
-
- procedure TForm1.DecryptBtnClick(Sender: TObject);
- var
- infile,
- outfile : TextFile;
- INfilename,
- OUTfilename : string;
- ch : char;
- begin
- INfilename := 'Encrypt.txt';
- OUTfilename := 'Decrypt.txt';
- if not FileExists( INfilename ) then { Check that input file exists }
- ShowMessage('File: ' + INfilename + ' not found!')
- else
- begin
- AssignFile(infile, INfilename);
- Reset(infile);
- AssignFile(outfile, OUTfilename);
- Rewrite(outfile);
- while not Eof(infile) do
- begin
- Read(infile, ch); (* To do a straight file copy *)
- { Write(outfile, ch); } (* Uncomment this line of code *)
- Write(outfile, Chr(Ord(ch)-MAGICNUM)); (* and comment out this line *)
- end;
- CloseFile(outfile);
- CloseFile(infile);
- end;
- end;
-
- end.
-